package dotdashdot.bradz.trendsettr;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.view.View;

import com.androidplot.ui.AnchorPosition;
import com.androidplot.ui.SizeLayoutType;
import com.androidplot.ui.SizeMetrics;
import com.androidplot.ui.XLayoutStyle;
import com.androidplot.ui.YLayoutStyle;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.PointLabelFormatter;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.XYStepMode;

public class ClassFormatGraph {
	XYPlot graph;
	LineAndPointFormatter formatter;
	int color;
	private ClassUberLogger logger;
	
	ClassFormatGraph(XYPlot graph, XYSeries values,int color){
		logger = new ClassUberLogger("ClassFormatGraph");
		this.graph = graph;
		this.color = color;
		init_formatter();
		apply_graph_rules();
		graph.addSeries(values, formatter);
		logger.info("Done with class init");
	}
	
	ClassFormatGraph(XYPlot graph, int color){
		logger = new ClassUberLogger("ClassFormatGraph");
		this.graph = graph;
		this.color = color;
		init_formatter();
		apply_graph_rules();
		logger.info("Done with class init");
	}
	
	public void add_series(XYSeries values){
		graph.addSeries(values, formatter);
	}
	
	public void init_formatter(){
		formatter = new LineAndPointFormatter();
		formatter.setPointLabelFormatter(new PointLabelFormatter());
		formatter.getLinePaint().setStrokeWidth(6f);
		//formatter.getLinePaint().setColor(context.getResources().getColor(R.color.navigation_color));
		formatter.getLinePaint().setColor(color);
		formatter.getVertexPaint().setColor(Color.TRANSPARENT);
		formatter.getFillPaint().setColor(Color.TRANSPARENT);
		formatter.getPointLabelFormatter().getTextPaint().setColor(Color.TRANSPARENT);
		
	}
	
	public void format_date(boolean date_same){
		if(!date_same){
			graph.setDomainStepValue(3);
		}else{
			graph.setDomainStepValue(2);
		}
	}
	
	public void apply_graph_rules(){
		//clear the previous info
		graph.clear();
        
        //Remove the legend
		graph.getLayoutManager().remove(graph.getLegendWidget());
		
		//Remove lines and format
		//Paint backgroundPaint = new Paint();
        //backgroundPaint.setColor(context.getResources().getColor(R.color.list_white));
        //backgroundPaint.setStyle(Paint.Style.FILL);
		
		//graph.getBorderPaint().setColor(context.getResources().getColor(R.color.list_green));
		graph.getBorderPaint().setStrokeWidth(3);
		graph.setBackgroundColor(Color.GRAY);
		
		/*View v = (View)graph.getParent();
		v.setBackgroundColor(Color.TRANSPARENT);
		v.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
		v.setDrawingCacheEnabled(false);
		v.setBackgroundResource(0);
		
		View v2 = (View)v.getParent();
		v2.setBackgroundColor(Color.TRANSPARENT);
		v2.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
		v2.setDrawingCacheEnabled(false);
		v2.setBackgroundResource(0);
		
		graph.getBorderPaint().setColor(Color.TRANSPARENT);
		graph.setBackgroundColor(Color.TRANSPARENT);
		graph.getBackgroundPaint().setColor(Color.TRANSPARENT);*/
		
		//graph.setBackgroundResource(0);
		//graph.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
		//graph.setDrawingCacheEnabled(true);
		//graph.setFocusable(false);
		//graph.setBackgroundPaint(backgroundPaint);
		graph.getGraphWidget().getBackgroundPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getCursorLabelBackgroundPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getCursorLabelPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getDomainGridLinePaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getDomainLabelPaint().setColor(Color.WHITE);
		graph.getGraphWidget().getDomainLabelPaint().setTextSize(20);

		//This prints 2 values on the x-axis. I guess the origin doesn't count...
		graph.setDomainStepValue(3);
		
		//This converts the timestamp into dates for the graph's x-axis
		graph.setDomainValueFormat(new Format() {
			 
            private SimpleDateFormat dateFormat = new SimpleDateFormat("M.d.yy");
 
            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {

                long timestamp = ((Number) obj).longValue();
                Date date = new Date(timestamp);
                return dateFormat.format(date, toAppendTo, pos);
            }
 
            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
 
            }

        });
		
		graph.getGraphWidget().getDomainOriginLabelPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getDomainSubGridLinePaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getGridBackgroundPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getRangeGridLinePaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getRangeLabelPaint().setColor(Color.WHITE);
		graph.getGraphWidget().getRangeLabelPaint().setTextSize(20);
		
		graph.setRangeBoundaries(0, 100, BoundaryMode.FIXED);
		graph.setRangeStepValue(3);
		graph.setRangeValueFormat(new DecimalFormat("0"));
		
		graph.getGraphWidget().getRangeOriginLabelPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().getRangeSubGridLinePaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().setRangeLabelOrientation(-400);
		
		
		graph.getRangeLabelWidget().getLabelPaint().setTextSize(20);
		graph.getRangeLabelWidget().setText("Votes");
		graph.getDomainLabelWidget().getLabelPaint().setTextSize(20);
		//graph.getDomainLabelWidget().setText("Time");
		//graph.getDomainLabelWidget().getLabelPaint().setColor(Color.TRANSPARENT);
		graph.getGraphWidget().setMarginTop(50);
		graph.getGraphWidget().setPaddingTop(10);
		graph.getGraphWidget().setMarginBottom(15);
		
		graph.getTitleWidget().getLabelPaint().setTextSize(30);
		graph.getTitleWidget().setMarginTop(20);
		graph.getTitleWidget().setMarginBottom(10);
		graph.getTitleWidget().setText("Popularity");
		
		logger.info("Done with graph formatting");
	}
	
}
